package com.culturalCenter.dataCenter.service.impl.site;

import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.site.BookingListResponse;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteBooking;
import com.culturalCenter.dataCenter.entity.site.SiteBooking;
import com.culturalCenter.dataCenter.mapper.site.SiteAlertsMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteBookingMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteMapper;
import com.culturalCenter.dataCenter.service.site.SiteBookingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/****
 * @Author:
 * @Description:SiteBooking业务层接口实现类
 * @Date
 *****/
@Service
public class SiteBookingServiceImpl implements SiteBookingService {

    @Resource
    private SiteBookingMapper siteBookingMapper;
    @Resource
    private SiteAlertsMapper siteAlertsMapper;
    @Resource
    private SiteMapper siteMapper;

    @Override
    public PageInfo<SiteBooking> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> siteBookingMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return siteBookingMapper.deleteById(id);
    }

    @Override
    public Integer update(SiteBooking siteBooking) {
        return siteBookingMapper.update(siteBooking);
    }

    @Override
    public Integer add(SiteBooking siteBooking) {
        return siteBookingMapper.insert(siteBooking);
    }

    @Override
    public SiteBooking findById(String id) {
        return siteBookingMapper.queryById(id);
    }

    @Override
    public List<SiteBooking> findAll(SiteBooking siteBooking) {
        return (List<SiteBooking>) siteBookingMapper.queryAll(siteBooking);
    }

    @Override
    public CustomSiteBooking getUsedInfo(String bookingId) {
        CustomSiteBooking customSiteBooking = new CustomSiteBooking();
        SiteBooking siteBooking = findById(bookingId);
        if (siteBooking != null) {
            BeanCopier beanCopier = BeanCopier.create(SiteBooking.class, CustomSiteBooking.class, false);
            beanCopier.copy(siteBooking, customSiteBooking, null);
            customSiteBooking.setAlertsList(siteAlertsMapper.getSiteAlertByTime(siteBooking.getSiteId(), siteBooking.getBeginTime(), siteBooking.getEndTime()));
        }

        String siteCode = siteMapper.getSiteCodeBySiteId(siteBooking.getSiteId());
//        String activityName = siteMapper.getActivityName(siteCode, siteBooking.getBeginTime(), siteBooking.getEndTime());
//        customSiteBooking.setActivityOrTrainingName(!activityName.isEmpty() ? activityName : siteMapper.getTrainingName(siteCode, siteBooking.getBeginTime(), siteBooking.getEndTime()));
        return customSiteBooking;
    }

    @Override
    public BookingListResponse getBookingList(String siteCode, Date date) {
//        String siteId = siteMapper.getSiteIdBySiteCode(siteCode);
        BookingListResponse listResponse = new BookingListResponse();

        // 年预约数
        listResponse.setTotal(siteBookingMapper.countTotalByDateAndStatus(siteCode, date));
        // 月预约数
        listResponse.setBookingNum(siteBookingMapper.countBookingNumByDateAndStatus(siteCode, date));
        // 预约列表
        List<CustomSiteBooking> bookingList = siteBookingMapper.selectSiteBookingByDateAndStatus(siteCode, date);
        Map<String, List<CustomSiteBooking>> siteBookingMap = new LinkedHashMap<>();
        List<CustomSiteBooking> bookingDayList;
        if (CollectionUtils.isNotEmpty(bookingList)) {
            for (CustomSiteBooking customSiteBooking : bookingList) {
                String key = DateFormatUtil.format(customSiteBooking.getBeginTime());
                if (!siteBookingMap.containsKey(key)) {
                    bookingDayList = new ArrayList<>();
                    bookingDayList.add(customSiteBooking);
                    siteBookingMap.put(key, bookingDayList);
                } else {
                    siteBookingMap.get(key).add(customSiteBooking);
                }
            }
        }
        listResponse.setBookingMap(siteBookingMap);
        return listResponse;
    }

}
